package org.jeecg.modules.util;

import org.jeecg.modules.demo.saobeiorderrecord.entity.SaobeiOrderRecord;
import org.jeecg.modules.demo.storeinfo.entity.StoreInfo;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class BeanUtils {


    public static <T, K, V> T map2Bean(Map<K, V> mp, Class<T> beanCls)
            {
        T t = null;
        try {

            BeanInfo beanInfo = Introspector.getBeanInfo(beanCls);
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            t = beanCls.newInstance();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (mp.containsKey(key)) {
                    Method setter = property.getWriteMethod();
                    Object value = mp.get(key);

                    try{
                        setter.invoke(t, value);
                    }catch (Exception e){

                    }
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return t;
    }

    // beanToMap方法：

    public static <T, K, V> Map<String, Object> bean2Map(T bean)
             {
        Map<String, Object> mp = new HashMap<>();
        if (bean == null) {
            return null;
        }

        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                if (!key.equals("class")) {
                    Method getter = property.getReadMethod();

                    Object value;

                    value = getter.invoke(bean);
                    mp.put(key, value);
                }
            }

        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return mp;
    }

    public static void fixAllFiledValue(Object bean)
    {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                if (!key.equals("class")) {
                    Method getter = property.getReadMethod();

                    Object value;

                    value = getter.invoke(bean);
                    if (value instanceof String){
                      String t=  ((String) value).replace("*","");
                        System.out.println(t);
                        Method setter = property.getWriteMethod();
                        if (setter != null){
                            setter.invoke(bean,new Object[]{t});
                        }else {
                            System.out.println("setter is null");
                        }
                    }

                }
            }

        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        StoreInfo s = new StoreInfo();
        s.setStoreName("*电动*");
        fixAllFiledValue(s);
        System.out.println(s.getStoreName());
    }
}
